/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package requeteur;

import evaluateur.Evaluator;
import evaluateur.QRelUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.DBWriter;
import model.RIModel;
import org.annolab.tt4j.TreeTaggerException;
import reppconn.LemmatiseurHandler;
import reppconn.StopList;

/**
 *
 * @author Nargisse
 */
public class Requeteur {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            
            ArrayList<String> termReqNonLem = new ArrayList<String>();
            //ArrayList<String> termReq = new ArrayList<String>(); 
            ArrayList<String> termReqLem = new ArrayList<String>();
            LemmatiseurHandler lemmise = new LemmatiseurHandler();
            StopList arrayStopList = new StopList();
            String currentToken = null; 
            //ArrayList<TermOccurReq> listTermeReq;
            TermeReqCollection listTermReq = new TermeReqCollection();
            DBWriter db = new DBWriter();
            ScoreByParagraphCalculator resultCalculator = new ScoreByParagraphCalculator(db);
            
            RIModel model = new RIModel();
            Evaluator eval = null;
            int nbRes = Integer.MAX_VALUE;
            
            //collecte des termes de la requete pas dans la stoplist et stockage en minuscule
            for(int i=0; i<args.length; i++){
                if(args[i].startsWith("qrel=")) {
                    eval = new Evaluator(QRelUtils.readQRelFile(args[i].substring(5), db));
                } else if(args[i].startsWith("nb_res=")) {
                    nbRes = Integer.parseInt(args[i].substring(7));
                } else if(!arrayStopList.isTokenStopList(args[i].toLowerCase())){
                    termReqNonLem.add(args[i].toLowerCase());
                    System.out.println("mot => "+ args[i].toLowerCase());
                }          
            }
            
            //lemmatisation
            lemmise.setListTermeNonLem(termReqNonLem);   
            lemmise.lemmatiser();   

            //stockage des termes lemmatisés
            for(int j=0; j < lemmise.getListTermeLem().size(); j++){
                currentToken = lemmise.getListTermeLem().get(j);

                if(!lemmise.isSmallerToken(currentToken)){
                    currentToken = lemmise.couperToken(currentToken);
                    termReqLem.add(currentToken);  
                    resultCalculator.addScoresOfTerme(currentToken);        
                }else{
                    termReqLem.add(currentToken);
                    resultCalculator.addScoresOfTerme(currentToken);
                }
            }
            
            //calcul des résultats
            resultCalculator.calculResults();
            ArrayList<Paragraphe> results = resultCalculator.getSortedResults(nbRes);
            
            System.out.println("=============================");
            System.out.println(" Résultats de la requête    =");
            System.out.println("=============================");
            //Affichage des résultats
            for (Paragraphe p : results) {
                System.out.println("P : id="+p.getIdParagraphe()+" score="+p.getPoidsParag()+" docId="+p.getDocId()+" xpath="+p.getXPath());
            }
            
            //Evaluation
            if (eval!=null) {
                System.out.println("\n");
                System.out.println("=============================");
                System.out.println(" Evaluation des résultats   =");
                System.out.println("=============================");
                System.out.println("Rappel : "+eval.getRappel(results));
                System.out.println("Précision : "+eval.getPrecision(results));
            }
            
            return;
            //
            
            
            //liste des termes de la requete lemmatises
            TermeReq t = null;
            for(String s : termReqLem){
                //System.out.println("LEMMA => "+s);
                //recup ds la BDD les termes qui sont identique a ceux de la requete
                t = model.addTermereq(s);
                
                if(t != null){
                    listTermReq.addTermeReq(t);
                    t.parsagePosition();
                }  
            }
            
           /* for(TermeReq tr : listTermReq.getListTermeReq()){
                //System.out.println("Val NoeudId = "+listTermeReq.get(n).getNodeId()+" Val Position = "+listTermeReq.get(n).getPosition());
                for(TermOccurReq tor : tr.getListTermOccuReq()){
                    System.out.println("Nom Terme = "+tor.getNameTerme()+" Val NoeudId = "+tor.getNodeId()+" Val Position = "+tor.getPosition()+" Val Poids = "+tor.getPoids());
                    
                }
            }*/
            
            listTermReq.addParagToHM();
            System.out.println(" size Liste P "+listTermReq.getListParg().size());
            
            
            for(Paragraphe p : listTermReq.getListParg()){
                
                System.out.println("IDPara = "+p.getIdParagraphe()+" Poids = "+p.getPoidsParag());
            }
            //Envoyer le resultat
            //Resultat result = new Resultat(listTermReq.getListParg());
            ArrayList<Resultat> resul;
            ArrayList<Double> listScore;
            resul = model.addResult(listTermReq.getListParg());
            System.out.println("SIZE RESULTAT "+resul.size());
            listScore = model.getListResult().sortResult(resul);
            System.out.println("List ordonnee =>"+listScore);
            //TODO : La liste est trie mais recup les paragraphe approprie...
            
        } catch (IOException ex) {
            Logger.getLogger(Requeteur.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TreeTaggerException ex) {
            Logger.getLogger(Requeteur.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
